//
//  WKWalletHistoryViewController.m
//  ourToken
//
//  Created by wakeup on 2018/6/5.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKWalletHistoryViewController.h"

@interface WKWalletHistoryViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) WKWalletHistoryHeaderView *headerView;//头部view
@property (nonatomic, strong) UIView *bottomView;//底部view
@property (nonatomic, strong) UIButton *transferBtn;//转账按钮
@property (nonatomic, strong) UIButton *receiptBtn;//收款按钮
@property (nonatomic, strong) NSMutableArray *walletHistoryArray;//数据源

@end

@implementation WKWalletHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationView];
    [self layoutUI];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getWalletTransactionListNetworking];
    [self.tableView registerNib:[UINib nibWithNibName:@"WKTransactionTableViewCell" bundle:nil] forCellReuseIdentifier:@"TransactionCell"];
}

- (void)setNavigationView {
    [super setNavigationView];
    self.navigationView.topTitleLabel.text = [NSString stringWithFormat:@"我的%@钱包",self.walletModel.coinType];
}

- (void)layoutUI {
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    [self.bottomView addSubview:self.transferBtn];
    [self.bottomView addSubview:self.receiptBtn];
}

//获取钱包交易列表
- (void)getWalletTransactionListNetworking {
    [httpManager getWalletTransactionListWithWalletId:self.walletModel.id coinType:self.walletModel.coinType pageIndex:1 pageSize:10 success:^(NSDictionary *result) {
        
        NSDictionary *data = [result objectForKey:@"data"];
        for (NSDictionary *dict in data) {
            WKTransactionModel *list = [WKTransactionModel mj_objectWithKeyValues:dict];
            [self.walletHistoryArray addObject:list];
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.walletHistoryArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WKTransactionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TransactionCell" forIndexPath:indexPath];
    WKTransactionModel *walletHistoryModel = self.walletHistoryArray[indexPath.row];
    cell.transactionModel = walletHistoryModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WKTransactionHistoryViewController *history = [[WKTransactionHistoryViewController alloc] init];
    [self.navigationController pushViewController:history animated:YES];
}

#pragma mark - Button点击事件
- (void)clickTransferBtn:(UIButton *)sender {
    WKTransferViewController *transfer = [[WKTransferViewController alloc] init];
    transfer.walletModel = self.walletModel;
    [self.navigationController pushViewController:transfer animated:YES];
}

- (void)clickReceiptBtn:(UIButton *)sender {
    WKReceiptViewController *receipt = [[WKReceiptViewController alloc] init];
    receipt.walletModel = self.walletModel;
    [self.navigationController pushViewController:receipt animated:YES];
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, AppManager.navigationHeight, AppManager.width, AppManager.height-AppManager.navigationHeight-AppManager.tabBarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.backgroundColor = [UIColor whiteColor];
//        _tableView.tableHeaderView = self.headerView;
    }
    return _tableView;
}

- (WKWalletHistoryHeaderView *)headerView {
    if (_headerView == nil) {
        _headerView = [[WKWalletHistoryHeaderView alloc] initWithFrame:CGRectMake(0, 0, AppManager.width, 200)];
        _headerView.backgroundColor = [UIColor greenColor];
    }
    return _headerView;
}

- (UIView *)bottomView {
    if (_bottomView == nil) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, AppManager.height-AppManager.tabBarHeight, AppManager.width, AppManager.tabBarHeight)];
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}

- (UIButton *)transferBtn {
    if (_transferBtn == nil) {
        _transferBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _transferBtn.frame = CGRectMake(0, 0, AppManager.width/2, AppManager.tabBarHeight);
        _transferBtn.backgroundColor = [UIColor whiteColor];
        [_transferBtn setTitle:@"转账" forState:UIControlStateNormal];
        [_transferBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _transferBtn.titleLabel.font = AppManager.btnFont;
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, 0, self.transferBtn.frame.size.width, 0.5);
        layer.backgroundColor = [[UIColor grayColor] CGColor];
        [_transferBtn.layer addSublayer:layer];
        [_transferBtn addTarget:self action:@selector(clickTransferBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _transferBtn;
}

- (UIButton *)receiptBtn {
    if (_receiptBtn == nil) {
        _receiptBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _receiptBtn.frame = CGRectMake(AppManager.width/2, 0, AppManager.width/2, AppManager.tabBarHeight);
        [_receiptBtn setTitle:@"收款" forState:UIControlStateNormal];
        [_receiptBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _receiptBtn.titleLabel.font = AppManager.btnFont;
        _receiptBtn.backgroundColor = AppManager.themeColor;
        [_receiptBtn addTarget:self action:@selector(clickReceiptBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _receiptBtn;
}

- (NSMutableArray *)walletHistoryArray {
    if (_walletHistoryArray == nil) {
        _walletHistoryArray = [[NSMutableArray alloc]init];
    }
    return _walletHistoryArray;
}

@end
